﻿using AutoMapper.Configuration;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Infrastructure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
using wt.wine.Application.UserTables;

namespace wt.wine.Infrastructure.Services
{
    public class TokenUserService : ITokenUserService
    {
      
        private readonly IConfiguration configuration;

        public TokenUserService(IConfiguration configuration)
        {            
            this.configuration = configuration;
        }
        public string CreateToken(Claim[] listClaim1, string[] Roles)
        {
            var Secret = configuration["Jwt:Secret"];
            var expires = configuration["Jwt:expires"];


            //秘钥，就是标头，这里用Hmacsha256算法，需要256bit的密钥
            var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Secret)), SecurityAlgorithms.HmacSha256);
            //Claim，JwtRegisteredClaimNames中预定义了好多种默认的参数名，也可以像下面的Guid一样自己定义键名.
            //ClaimTypes也预定义了好多类型如role、email、name。Role用于赋予权限，不同的角色可以访问不同的接口
            //相当于有效载荷
            //var claims = new Claim[] {
            //    new Claim(JwtRegisteredClaimNames.Iss,jwtConfig.GetValue<string>("Iss")),
            //    new Claim(JwtRegisteredClaimNames.Aud,jwtConfig.GetValue<string>("Aud")),
            //    new Claim("Guid",Guid.NewGuid().ToString("D")),
            //    new Claim("NameUser",uname),
            //    new Claim(ClaimTypes.Role,"system"),
            //    new Claim(ClaimTypes.Role,"admin"),
            //  };
            List<Claim> listClaim = listClaim1.ToList();
            //listClaim.Add(new Claim(JwtRegisteredClaimNames.Iss, jwtConfig.GetValue<string>("Iss")));
            //listClaim.Add(new Claim(JwtRegisteredClaimNames.Aud, jwtConfig.GetValue<string>("Aud")));
            listClaim.Add(new Claim("uid", Guid.NewGuid().ToString("D")));
            foreach (var item in Roles)
            {
                listClaim.Add(new Claim(ClaimTypes.Role, item));
            }


            SecurityToken securityToken = new JwtSecurityToken(
                signingCredentials: securityKey,
                expires: DateTime.Now.AddMinutes( Convert.ToInt32(expires)),//过期时间
                claims: listClaim
            );
            //生成jwt令牌
            return new JwtSecurityTokenHandler().WriteToken(securityToken);
        }

        public string CreateToken(LoginDTO loginResult)
        {

            var Secret = configuration["Jwt:Secret"];
            var expires = configuration["Jwt:expires"];


            //秘钥，就是标头，这里用Hmacsha256算法，需要256bit的密钥
            var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Secret)), SecurityAlgorithms.HmacSha256);
            List<Claim> listClaim = new List<Claim>();
            listClaim.Add(new Claim("id", loginResult.id.ToString()));//用户id
            listClaim.Add(new Claim("account", loginResult.account));//账号
            listClaim.Add(new Claim("roleid", loginResult.roleid.ToString()));//角色id
            listClaim.Add(new Claim("name", loginResult.name));//用户名
            listClaim.Add(new Claim("address", loginResult.address));//地址
            listClaim.Add(new Claim("roletype", loginResult.roletype.ToString()));//类型
            listClaim.Add(new Claim("leroleNamevel", loginResult.roleName));//角色名称
            SecurityToken securityToken = new JwtSecurityToken(
                signingCredentials: securityKey,
                expires: DateTime.Now.AddMinutes(Convert.ToInt32(expires)),//过期时间
                claims: listClaim
            );
            //生成jwt令牌
            return new JwtSecurityTokenHandler().WriteToken(securityToken);
        }
    }
}
